package cn.zhaoyuening.zshop4j.common.utils;

import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.IOException;

/**
 * Created by Zhao on 2017/4/8.
 * 封装fast client
 * 需要properties/config.properties tracker_serve img_host_address
 */
public class FastFdsClientUtils {
    private TrackerClient trackerClient;
    private TrackerServer trackerServer;
    private StorageServer storageServer;
    private StorageClient1 storageClient1;

    private String fastFdsDownloadAddress;


    public void init() throws IOException, MyException {
        String path = this.getClass().getClassLoader().getResource("properties/config.properties").getFile();
        ClientGlobal.init(path);
        this.trackerClient = new TrackerClient();
        this.trackerServer = trackerClient.getConnection();
        this.storageServer = null;
        this.storageClient1 = new StorageClient1(trackerServer, storageServer);
    }

    public void destroy() throws IOException {
        if (this.trackerServer!=null)
            this.trackerServer.close();
        if (this.storageServer!=null)
            this.storageServer.close();
    }

    /**
     * 上传图片
     * @param localFilePath 本地图片路径
     * @return 成功返回图片url 失败返回null
     */
    public String uploadFile(String localFilePath) {
        String fileExtName = getExtName(localFilePath);
        try {
            String[] strings = storageClient1.upload_file(localFilePath, fileExtName, null);
            StringBuffer sb = new StringBuffer();
            sb.append(fastFdsDownloadAddress);
            for (String str :
                    strings) {
                sb.append("/" + str);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传图片
     * @param bytes
     * @param fileName
     * @return 成功返回图片url 失败返回null
     */
    public String uploadFile(byte[] bytes,String fileName) {
        String fileExtName = getExtName(fileName);
        try {
            String[] strings = storageClient1.upload_file(bytes, fileExtName, null);
            StringBuffer sb = new StringBuffer();
            sb.append(fastFdsDownloadAddress);
            for (String str :
                    strings) {
                sb.append("/" + str);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void setFastFdsDownloadAddress(String fastFdsDownloadAddress) {
        this.fastFdsDownloadAddress = fastFdsDownloadAddress;
    }



    //获取后缀名
    private String getExtName(String fileName) {
        int index = fileName.lastIndexOf(".");
        if (index < 0) {
            return "";
        }
        String extName = fileName.substring(index + 1);
        return extName;
    }
}
